import {
  addTeacher,
  deleteTeacher,
  queryTeacher,
  editPassword,
  editTeacher,
} from '../services/teacher';

export default {
  namespace: 'teacher',

  state: {
    teacherList: [],
  },

  effects: {
    *fetchTeacherList({ payload }, { call, put }) {
      const dataLsit = yield call(queryTeacher, payload);
      if (dataLsit.status) {
        yield put({
          type: 'saveTeacherList',
          payload: dataLsit.data,
        });
      }
    },
    *addTeacher({ payload }, { call, put }) {
      const data = yield call(addTeacher, payload);
      if (data.status) {
        const dataLsit = yield call(queryTeacher, {});
        yield put({
          type: 'saveTeacherList',
          payload: dataLsit.data,
        });
      }
    },
    *removeTeacher({ payload }, { call, put }) {
      const data = yield call(deleteTeacher, payload);
      if (data.status) {
        const dataLsit = yield call(queryTeacher, {});
        yield put({
          type: 'saveTeacherList',
          payload: dataLsit.data,
        });
      }
    },
    *updateTeacher({ payload }, { call, put }) {
      const data = yield call(editTeacher, payload);
      if (data.status) {
        const dataLsit = yield call(queryTeacher, {});
        yield put({
          type: 'saveTeacherList',
          payload: dataLsit.data,
        });
      }
    },
    *updatePassword({ payload }, { call, put }) {
      const data = yield call(editPassword, payload);
      if (data.status) {
        yield put({
          type: '',
          payload: '',
        });
      }
    },
  },

  reducers: {
    saveTeacherList(state, { payload }) {
      return {
        ...state,
        teacherList: payload,
      };
    },
  },
};
